package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanmao.blog.entity.Views;
import com.lanmao.blog.service.ViewsService;
import com.lanmao.blog.utils.R;
import com.lanmao.blog.vo.WeekViews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/views")
public class ViewsController {
    @Autowired
    ViewsService viewsService;

    /**
     * 查询所有的访问量
     * @return
     */
    @RequestMapping("getAllViews")
    public R getAllViews(){
        String views = viewsService.listofViews();
        return R.ok().data("data",views);
    }

    /**
     * 查询以天为单位的7天访问量
     * @return
     */
    @RequestMapping("getViewsbyWeek")
    public R getViewsbyWeek(){

        List<WeekViews> list = viewsService.getByweek();
        return R.ok().data("data",list);
    }

    /**
     *获取当天总浏览量
     * @return
     */
    @RequestMapping("getTodayViews")
    public R getTodayViews(){

        WeekViews weekViews = viewsService.getTodayViews();

        return R.ok().data("data",weekViews);
    }
    /**
     * 查看该用户当天访问数
     */
    @RequestMapping("getTodayViewsByUser")
    public R getTodayViewsByUser(String id){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = format.format(date);
        Views views = viewsService.getOne(new QueryWrapper<Views>().eq("date_time",s).eq("user_id",id));

        return R.ok().data("data",views);
    }
}

